﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace WebApp.Settings
{
    public class JsonSettings
    {
        public string RealTimeTrackingUrl { get; set; }
        public string ReportsUrl { get; set; }
        public ExistingAppMenus ExistingAppMenuNames { get; set; }
        public List<NewMenu> WelcomeMenuNames { get; set; }
        public Footer FooterLinks { get; set; }

        public JsonSettings()
        {
            this.RealTimeTrackingUrl = "http://daycarephp.azurewebsites.net/";
            this.ReportsUrl = "http://161.202.19.117:8089/Reports/Pages/Folder.aspx";
            this.ExistingAppMenuNames = new ExistingAppMenus();
            this.WelcomeMenuNames = new List<NewMenu>();
            this.WelcomeMenuNames.Add(new NewMenu { Name = "About", Url = "http://accufindgroup.com/" });
            this.FooterLinks = new Footer();
        }
        
        public static JsonSettings ReadFromJson()
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            JsonSettings settings = new JsonSettings();
            try
            {
                string data = File.ReadAllText(HttpContext.Current.Server.MapPath("~/Settings/settings.json"));
                settings = serializer.Deserialize<JsonSettings>(data);
                if(settings== null)
                {
                    settings = new JsonSettings();
                }
            }   
            catch(Exception ex)
            {
                settings = new JsonSettings();
            }
            return settings;
        }
    }

    public class ExistingAppMenus
    {
        public string ClientManagement { get; set; }
        public string RealtimeTracking { get; set; }
        public string ReportManagement { get; set; }
        public string UserManagement { get; set; }
        public ExistingAppMenus()
        {
            this.ClientManagement = "Client Management";
            this.RealtimeTracking = "Real-time Tracking";
            this.ReportManagement = "Report Management";
            this.UserManagement = "User Management";
        }
    }

    public class Footer
    {
        public NewMenu CopyRightLink { get; set; }
        public NewMenu TermsOfService { get; set; }
        public Footer()
        {
            this.CopyRightLink = new NewMenu { Name= "Accufind Technologies Pte. Ltd.", Url= "http://accufindgroup.com/" };
            this.TermsOfService = new NewMenu();
        }
    }

    public class NewMenu
    {
        public string Name { get; set; }
        public string Url { get; set; }
        public NewMenu()
        {
            this.Name = "";
            this.Url = "";
        }

        public bool IsEmpty()
        {
            bool result = true;
            if(!string.IsNullOrWhiteSpace(this.Name) && !string.IsNullOrWhiteSpace(this.Url))
            {
                result = false;
            }
            return result;
        }
    }
}